package com.gitee.huanminabc.validate;


import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;

public class ListStrValueValidator implements ConstraintValidator<ListStrValue,String> {

    private final Set<String> set = new HashSet<>();
    //初始化方法
    @Override
    public void initialize(ListStrValue constraintAnnotation) {
        String[] vals = constraintAnnotation.vals();
        set.addAll(Arrays.asList(vals));
    }

    //判断是否校验成功
    /**
     *
     * @param value 需要校验的值
     * @param context
     * @return
     */
    @Override
    public boolean isValid(String value, ConstraintValidatorContext context) {

        return set.contains(value);
    }
}
